package com.example.finalwork06.Common;

import com.example.finalwork06.Entity.*;
import com.example.finalwork06.Mapper.*;
import com.example.finalwork06.Service.CrawlerLogService;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Component
public class ScheduledDemo {
    // 定义多个爬虫服务接口的实例
    @Autowired
    private  CrawlerLogService crawlerLogService;
    @Autowired
    private ChinaIndexMapper chinaIndexMapper;
    @Autowired
    private SectorCapitalFlowsMapper sectorCapitalFlowsMapper;
    @Autowired
    private SecuritiesAlertsMapper securitiesAlertsMapper;
    @Autowired
    private NewSharesMapper newSharesMapper;
    @Autowired
    private FundFlowsMapper fundFlowsMapper;


    // 创建一个线程池
    private final ExecutorService executorService = Executors.newFixedThreadPool(5); // 假设要爬取5个网站

    // 使用构造函数注入依赖

    @Scheduled(fixedDelay = 60000) // 每分钟执行一次
    public void runCrawler1() {
        executorService.execute(() -> {
            try {
                CrawlerLog crawlerLog=new CrawlerLog();
                LocalDateTime startTime=LocalDateTime.now();
                String url = "http://quote.eastmoney.com/center/qqzs.html";

                // HtmlUnit 模拟浏览器
                WebClient webClient = new WebClient(BrowserVersion.CHROME);
                webClient.getOptions().setJavaScriptEnabled(true);
                webClient.getOptions().setCssEnabled(false);
                webClient.getOptions().setThrowExceptionOnScriptError(false);
                webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
                webClient.getOptions().setTimeout(10 * 1000);
                HtmlPage page = null;
                try {
                    page = webClient.getPage(url);
                    webClient.waitForBackgroundJavaScript(30 * 1000);
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }
                String pageAsXml = page.asXml();

                // Jsoup解析处理
                Document doc = Jsoup.parse(pageAsXml, "http://quote.eastmoney.com/center/qqzs.html");

                Element element = doc.getElementById("qqzs_asia_simple-table");
                if (element != null) {
                    Elements elements = element.getElementsByTag("tr");
                    int i = 0;
                    for (Element el : elements) {
                        String name = el.getElementsByTag("a").text();
                        Element moneyElement = el.select("td.mywidth2 span").first();
                        if (moneyElement != null) {
                            String money = moneyElement.text();
                            Double money1 = Double.parseDouble(money);
                            ChinaIndex chinaIndex = new ChinaIndex();
                            chinaIndex.setIName(name);
                            chinaIndex.setIPrice(money1);
                            boolean flag = chinaIndexMapper.selectByName(name);
                            if (flag) {
                                chinaIndexMapper.updatePrice(money1,name);

                            } else {
                                chinaIndexMapper.insertChinaIndex(chinaIndex);
                            }
                            i++;
                            if (i == 9)
                                break;
                        }
                    }
                }
                LocalDateTime endTime = LocalDateTime.now();
                crawlerLog.setActionDescription("正在爬取中国指数相关信息");
                crawlerLog.setDataTableName("China_Index表");
                crawlerLog.setStartTime(startTime);
                crawlerLog.setEndTime(endTime);
                crawlerLog.setWebsiteUrl(url);
                crawlerLogService.crawAndSaveData(crawlerLog);
            } catch (Exception e) {
                e.printStackTrace(); // 处理异常，记录日志或采取其他措施
            }
        });
    }

    @Scheduled(fixedDelay = 60000) // 每分钟执行一次
    public void runCrawler2() {
        executorService.execute(() -> {
            try {
                CrawlerLog crawlerLog=new CrawlerLog();
                LocalDateTime startTime=LocalDateTime.now();

                String url="http://vip.stock.finance.sina.com.cn/corp/go.php/vRPD_NewStockIssue/page/1.phtml";
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                // HtmlUnit 模拟浏览器
                WebClient webClient = new WebClient(BrowserVersion.CHROME);
                webClient.getOptions().setJavaScriptEnabled(true);              // 启用JS解释器，默认为true
                webClient.getOptions().setCssEnabled(false);                    // 禁用css支持
                webClient.getOptions().setThrowExceptionOnScriptError(false);   // js运行错误时，是否抛出异常
                webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
                webClient.getOptions().setTimeout(10 * 1000);                   // 设置连接超时时间
                HtmlPage page = webClient.getPage(url);
                webClient.waitForBackgroundJavaScript(5 * 1000);               // 等待js后台执行30秒
                boolean status=true;
                while (true){
                    String pageAsXml = page.asXml();
                    Document doc = Jsoup.parse(pageAsXml, "http://vip.stock.finance.sina.com.cn/corp/go.php/vRPD_NewStockIssue/page/1.phtml");

                    Document document= Jsoup.parse(new URL(url),3000);
//        System.out.println(document);
                    Element element=doc.getElementById("NewStockTable");
                    Elements elements=element.getElementsByTag("tr");
                    for (Element el:elements){
                        Elements rows=el.getElementsByTag("td");
                        String name=new String();
                        String date=new String();
                        if(rows.size()>=5){
                            name=rows.get(2).text();
                            date=rows.get(4).text();
                            if (date.isEmpty()){
                                continue;
                            }
                            try {
                                Calendar calendar = Calendar.getInstance();
                                Date dateToCompare = sdf.parse(date);
                                Date currentDate = new Date();
                                calendar.setTime(currentDate);

                                // 获取当前日期的半年前日期
                                calendar.add(Calendar.MONTH, -6);
                                Date MonthsAgo = calendar.getTime();

                                if (dateToCompare.before(MonthsAgo)) {
                                    status=false;
                                    break;
                                }

                            } catch (ParseException e) {
                                continue;
                            }
                            System.out.println( "名称："+name+";上市时间："+date);
                            NewShares newShares=new NewShares();
                            newShares.setSName(name);
                            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                            Date time = formatter.parse(date);
                            newShares.setSDate(time);

                            boolean flag = newSharesMapper.selectByName(name);
                            if (flag) {
                                newSharesMapper.updateShares(time,name);
                            } else {
                                newSharesMapper.insertNewShares(newShares);
                            }
                        }
                    }
                    if (!status){
                        break;
                    }
                    HtmlAnchor nextPageAnchor = (HtmlAnchor) page.getFirstByXPath("//a[contains(text(),'下一页')]");
                    page = nextPageAnchor.click();
                    webClient.waitForBackgroundJavaScript(5 * 1000); // 等待 js 后台执行

                }
                webClient.close();
                LocalDateTime endTime = LocalDateTime.now();
                crawlerLog.setActionDescription("正在爬取新股上市日历相关信息");
                crawlerLog.setDataTableName("New_Shares_Calendar表");
                crawlerLog.setStartTime(startTime);
                crawlerLog.setEndTime(endTime);
                crawlerLog.setWebsiteUrl(url);
                crawlerLogService.crawAndSaveData(crawlerLog);
            } catch (Exception e) {
                e.printStackTrace(); // 处理异常，记录日志或采取其他措施
            }
        });
    }

    @Scheduled(fixedDelay = 60000) // 每分钟执行一次
    public void runCrawler3() {
        executorService.execute(() -> {
            try {
                CrawlerLog crawlerLog=new CrawlerLog();
                LocalDateTime startTime=LocalDateTime.now();

                String url="https://kuaixun.eastmoney.com/";

                System.out.println("Loading page now-----------------------------------------------: "+url);

                // HtmlUnit 模拟浏览器
                WebClient webClient = new WebClient(BrowserVersion.CHROME);
                webClient.getOptions().setJavaScriptEnabled(true);              // 启用JS解释器，默认为true
                webClient.getOptions().setCssEnabled(false);                    // 禁用css支持
                webClient.getOptions().setThrowExceptionOnScriptError(false);   // js运行错误时，是否抛出异常
                webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
                webClient.getOptions().setTimeout(10 * 1000);                   // 设置连接超时时间
                HtmlPage page = webClient.getPage(url);
                webClient.waitForBackgroundJavaScript(10 * 1000);               // 等待js后台执行30秒

                String pageAsXml = page.asXml();

                Document doc = Jsoup.parse(pageAsXml, "https://kuaixun.eastmoney.com/");

                Element element=doc.getElementById("news_item_collection");
                Element timeEl=element.selectFirst("div.news_time");
                Element titleEl=element.selectFirst("span.news_detail_text.font_3, span.news_detail_text");
                String classname=titleEl.className();
                //类型函数
                Integer status;
                String originalString=titleEl.text();
                String title="";
                // 检查字符串是否包含括号
                if (originalString.contains("【") && originalString.contains("】")) {
                    // 查找开头的特殊字符
                    int startIndex = originalString.indexOf("【");
                    // 查找结尾的特殊字符
                    int endIndex = originalString.indexOf("】") + 1; // 加 1 是为了包括结尾的 '】'

                    if (startIndex != -1 && endIndex != -1 && startIndex < endIndex) {
                        // 提取特定部分的字符串
                        title = originalString.substring(startIndex, endIndex);

                    } else {
                        System.out.println("未找到指定的字符串部分。");
                    }
                } else {
                    // 如果不包含括号，则获取整个字符串
                    title=originalString;
                }
                String time=timeEl.text();
                if("news_detail_text font_3".equals(classname)){
                    status=0;
                }else {
                    status=1;
                }
                SecuritiesAlerts securitiesAlerts=new SecuritiesAlerts();
                securitiesAlerts.setSaTime(time);
                securitiesAlerts.setStatus(status);
                securitiesAlerts.setSaTitle(title);

                boolean flag = securitiesAlertsMapper.selectByName(title);
                if (flag) {
                    System.out.println();
                } else {
                    securitiesAlertsMapper.insertSecuritiesAlerts(securitiesAlerts);
                }
                System.out.println(titleEl.text());
                System.out.println(timeEl.text()+";"+status+";"+classname);

                LocalDateTime endTime = LocalDateTime.now();
                crawlerLog.setActionDescription("正在爬取证券快讯相关信息");
                crawlerLog.setDataTableName("Securities_Alters表");
                crawlerLog.setStartTime(startTime);
                crawlerLog.setEndTime(endTime);
                crawlerLog.setWebsiteUrl(url);
                crawlerLogService.crawAndSaveData(crawlerLog);
            } catch (Exception e) {
                e.printStackTrace(); // 处理异常，记录日志或采取其他措施
            }
        });
    }

    @Scheduled(fixedDelay = 60000) // 每分钟执行一次
    public void runCrawler5() {
        executorService.execute(() -> {
            try {
                CrawlerLog crawlerLog=new CrawlerLog();
                LocalDateTime startTime=LocalDateTime.now();
                String url = "https://data.eastmoney.com/bkzj/hy.html";

                // HtmlUnit 模拟浏览器
                WebClient webClient = new WebClient(BrowserVersion.CHROME);
                webClient.getOptions().setJavaScriptEnabled(true);              // 启用JS解释器，默认为true
                webClient.getOptions().setCssEnabled(false);                    // 禁用css支持
                webClient.getOptions().setThrowExceptionOnScriptError(false);   // js运行错误时，是否抛出异常
                webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
                webClient.getOptions().setTimeout(10 * 1000);                   // 设置连接超时时间

                HtmlPage page = webClient.getPage(url);
                webClient.waitForBackgroundJavaScript(60 * 1000);               // 等待js后台执行30秒

                int pageNumber = 1;
                while (true) {
                    System.out.println("Processing page " + pageNumber);
                    String pageAsXml = page.asXml();
                    Document doc = Jsoup.parse(pageAsXml, url);

                    Element element = doc.getElementById("dataview");
                    if (element != null) {
                        Elements elements = element.getElementsByTag("tr");
                        for (Element row : elements) {
                            Elements clo=row.getElementsByTag("td");
                            if(clo.size()>5){
                                String name=clo.get(1).text();
                                String flows=clo.get(3).text();
                                System.out.println(name+";"+flows);
                                SectorCapitalFlows sectorCapitalFlows=new SectorCapitalFlows();
                                sectorCapitalFlows.setScfName(name);
                                sectorCapitalFlows.setScfFlows(flows);
                                boolean flag = sectorCapitalFlowsMapper.selectByName(name);
                                if (flag) {
                                    sectorCapitalFlowsMapper.updateFlows(flows,name);

                                } else {
                                    sectorCapitalFlowsMapper.insertSectorCapitalFlows(sectorCapitalFlows);
                                }
                            }

                        }
                    }

                    // 查找“下一页”按钮
                    HtmlAnchor nextPageAnchor = (HtmlAnchor) page.getFirstByXPath("//a[@data-page='2' and contains(text(),'下一页')]");
                    System.out.println(nextPageAnchor);
                    if (nextPageAnchor == null) {
                        break; // 没有找到“下一页”按钮，结束循环
                    }
                    page = nextPageAnchor.click();
                    webClient.waitForBackgroundJavaScript(30 * 1000); // 等待 js 后台执行
                    pageNumber++;
                }

                webClient.close();

                LocalDateTime endTime = LocalDateTime.now();
                crawlerLog.setActionDescription("正在爬取板块资金流今日涨跌幅相关信息");
                crawlerLog.setDataTableName("Sector_Capital_Flows表");
                crawlerLog.setStartTime(startTime);
                crawlerLog.setEndTime(endTime);
                crawlerLog.setWebsiteUrl(url);
                crawlerLogService.crawAndSaveData(crawlerLog);
            } catch (Exception e) {
                e.printStackTrace(); // 处理异常，记录日志或采取其他措施
            }
        });
    }


    @Scheduled(fixedDelay = 60000) // 每分钟执行一次
    public void runCrawler4() {
        executorService.execute(() -> {
            try {

                CrawlerLog crawlerLog=new CrawlerLog();
                LocalDateTime startTime=LocalDateTime.now();

                FundFlows flows=new FundFlows();

                // 设置 ChromeDriver 的路径
                System.setProperty("webdriver.chrome.driver", "D:\\chromedriver-win64\\chromedriver.exe");

                // 设置 ChromeOptions
                ChromeOptions options = new ChromeOptions();
                options.addArguments("--headless"); // 无头模式

                // 启动 ChromeDriver
                WebDriver driver = new ChromeDriver(options);
                driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
                driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);

                String url = "https://data.eastmoney.com/hsgt/hsgtV2.html";
                System.out.println("Loading page now-----------------------------------------------: " + url);
                driver.get(url);

                // 获取页面源代码
                String pageSource = driver.getPageSource();
                driver.quit();

                // 使用 Jsoup 解析页面源代码
                Document doc = Jsoup.parse(pageSource, url);
                Element tableElement = doc.select("tbody.hk_table").first();
//        System.out.println(tableElement);
                Element element=doc.select("tbody.north_table").first();
                Elements tableElements=tableElement.getElementsByTag("tr");
                Elements elements=element.getElementsByTag("tr");
                boolean first=true;
                for (Element el : tableElements) {
//            System.out.println(el.text()); // 打印元素HTML内容
                    Elements rows=el.getElementsByTag("td");
//            System.out.println(rows.size());
                    //名称
                    String name;
                    //剩余
                    String balance;
                    //已用
                    String used;
                    //成交
                    String netPurchases;
                    if(first){
                        name=rows.get(1).text();
                        balance=rows.get(5).text();
                        used=rows.get(4).text();
                        netPurchases=rows.get(3).text();
                        System.out.println(name+","+balance+","+used+","+netPurchases);
                        first=false;
                    }else {
                        name=rows.get(0).text();
                        balance=rows.get(4).text();
                        used=rows.get(3).text();
                        netPurchases=rows.get(2).text();
                        System.out.println(name+","+balance+","+used+","+netPurchases);
                    }
                    flows.setScffName(name);
                    flows.setScffBalance(balance);
                    flows.setScffUsed(used);
                    flows.setScffNetPurchases(netPurchases);

                    if (fundFlowsMapper.selectByName(name)){
                        fundFlowsMapper.updateByName(flows);
                    }else {
                        fundFlowsMapper.insertFundFlows(flows);
                    }


                }
//        System.out.println(elements.html());
//                first=true;
//                for (Element el :elements){
//                    //            System.out.println(el.text()); // 打印元素HTML内容
//                    Elements rows=el.getElementsByTag("td");
////            System.out.println(rows.size());
//                    //名称
//                    String name;
//                    //剩余
//                    String balance;
//                    //已用
//                    String used;
//                    //成交
//                    String netPurchases;
//                    if(rows.size()>5){
//                        if(first){
//                            name=rows.get(1).text();
//                            balance=rows.get(5).text();
//                            used=rows.get(4).text();
//                            netPurchases=rows.get(3).text();
//                            System.out.println(name+","+balance+","+used+","+netPurchases);
//                            first=false;
//                        }else {
//                            name=rows.get(0).text();
//                            balance=rows.get(4).text();
//                            used=rows.get(3).text();
//                            netPurchases=rows.get(2).text();
//                            System.out.println(name+","+balance+","+used+","+netPurchases);
//                        }
//                        flows.setScffName(name);
//                        flows.setScffBalance(balance);
//                        flows.setScffUsed(used);
//                        flows.setScffNetPurchases(netPurchases);
//
//                        if (fundFlowsMapper.selectByName(name)){
//                            fundFlowsMapper.updateByName(flows);
//                        }else {
//                            fundFlowsMapper.insertFundFlows(flows);
//                        }
//                    }
//
//
//                }



                LocalDateTime endTime = LocalDateTime.now();
                crawlerLog.setActionDescription("正在爬取沪深港通资金流相关信息");
                crawlerLog.setDataTableName("Stock_CONNECT_FUND_FLOWS表");
                crawlerLog.setStartTime(startTime);
                crawlerLog.setEndTime(endTime);
                crawlerLog.setWebsiteUrl(url);
                crawlerLogService.crawAndSaveData(crawlerLog);
            } catch (Exception e) {
                e.printStackTrace(); // 处理异常，记录日志或采取其他措施
            }
        });
    }


}
